package com.sp.system.user.core.security.handler;

import com.sp.system.user.core.exception.TokenVerifyException;
import com.sp.system.user.core.utils.enums.PermissionEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description: 认证失败处理类
 *               在访问一个受保护的资源，用户没有通过登录认证
 * @Author: chenanhai
 * @Date: 2019年9月3日
 */
@Slf4j
@Component
public class MyAuthenticationEntryPointHandler implements AuthenticationEntryPoint{
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        log.error("接口访问：{{}}，访问失败，请核对接口信息，重新认证",httpServletRequest.getRequestURI());
        throw new TokenVerifyException(e.getMessage(),PermissionEnum.VERIFY_ERROR.getStatus());
    }
}
